package com.xq.chrome.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Rest 模版调用测试
 * 将演示系统之间如何发起rest请求
 *
 * @author MACHENIKE
 */
@RestController
@RequestMapping("restCall")
public class RestClientTestController {
  /**
   * spring框架提供的rest辅助类
   */
  @Autowired
  RestTemplateBuilder restTemplateBuilder;

  /**
   * 欲访问的的api 的基础域名
   */
  @Value("${api.user}")
  String base;

  /**
   * 欲获取的用户id
   *
   * @param id
   */
  @GetMapping("/get/{id}")
  public @ResponseBody String getUser(@PathVariable String id) {
    System.out.println(id);
    RestTemplate restTemplate = restTemplateBuilder.build();
    String uri = base + "/user/ping";
    String result = restTemplate.getForObject(uri, String.class);
    System.out.println(result);
    return result;
  }

  /**
   * 欲获取的用户id
   *
   */
  @GetMapping("/ping")
  public @ResponseBody
  String ping() {
    return "ok";
  }


}
